package com.regal.packet;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Collection;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.xmlpull.v1.XmlPullParser;

public class StockQuery extends IQ {

	private final Map<String,Collection<ComQueryItem>> comMap = new HashMap<String,Collection<ComQueryItem>>();

	private String stockid;

	private String companyname;

	private String tradename;

	private String city;

	/**
	 * Element name of the packet extension.
	 */
	public static final String ELEMENT_NAME = "query";

	/**
	 * Namespace of the packet extension.
	 */
	public static final String NAMESPACE = "jabber:iq:stock_query";

	public void addComQueryItem(String tradeId,ComQueryItem item) {
		synchronized (comMap) {
			if(comMap.containsKey(tradeId)){
				Collection<ComQueryItem> items = comMap.get(tradeId);
				items.add(item);
			}
			else{
				Collection<ComQueryItem> items = new ArrayList<ComQueryItem>();
				items.add(item);
				comMap.put(tradeId,items);
			}
		}
	}

	public int getCompanyItemCount() {
		synchronized (comMap) {
			return comMap.size();
		}
	}

	public Map<String,Collection<ComQueryItem>> getCompanyItems() {
		synchronized (comMap) {
			return comMap;
		}
	}

	public String getChildElementXML() {
		StringBuffer buf = new StringBuffer();
		buf.append("<query xmlns=\"" + NAMESPACE + "\">");
		if(stockid != null){
			buf.append("<stockid>" + stockid + "</stockid>");
		}
		buf.append("</query>");
		return buf.toString();

	}

	/**
	 * A Company item, which consists of a companyid...
	 */
	public static class ComQueryItem {

		private String stockid;

		private String stockname;
		
		private String email;
		
		public ComQueryItem() {

		}

		/**
		 * Creates a new company item.
		 * 
		 * @param user
		 *            the companyid.
		 * @param name
		 *            the companyname.
		 */
		public ComQueryItem(String stockid, String stockname, String email) {
			this.stockid = stockid;
			this.stockname = stockname;
			this.email = email;
		}

		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public String getStockid() {
			return stockid;
		}

		public void setStockid(String stockid) {
			this.stockid = stockid;
		}

		public String getStockname() {
			return stockname;
		}

		public void setStockname(String stockname) {
			this.stockname = stockname;
		}
	}

	/**
	 * An IQProvider for CompanyListIQ packet.
	 * 
	 * @author Derek DeMoro
	 */
	public static class Provider implements IQProvider {

		public Provider() {
			super();
		}

		public IQ parseIQ(XmlPullParser parser) throws Exception {

			StockQuery comQueryList = new StockQuery();
			ComQueryItem comQueryItem = null;
			
			String tradeId = null;

			boolean done = false;
			while (!done) {
				int eventType = parser.next();
				if (eventType == XmlPullParser.START_TAG) {
					if (parser.getName().equals("item")) {
						comQueryItem = new ComQueryItem();
						tradeId = parser.getAttributeValue("", "tradeid");
						String stockId = parser.getAttributeValue("", "stockid");
						comQueryItem.setStockid(stockId);
						String stockName = parser.getAttributeValue("","stockname");
						comQueryItem.setStockname(stockName);
						String email = parser.getAttributeValue("","email");
						comQueryItem.setEmail(email);
					}
				}

				else if (eventType == XmlPullParser.END_TAG) {
					if (parser.getName().equals("item")) {
						comQueryList.addComQueryItem(tradeId,comQueryItem);
						System.out.println("add query item "+tradeId+":"+comQueryItem.getStockname());
					}
					if (parser.getName().equals(ELEMENT_NAME)) {
						done = true;
					}
				}
			}
			return comQueryList;
		}
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCompanyname() {
		return companyname;
	}

	public void setCompanyname(String companyname) {
		this.companyname = companyname;
	}

	public String getStockid() {
		return stockid;
	}

	public void setStockid(String stockid) {
		this.stockid = stockid;
	}

	public String getTradename() {
		return tradename;
	}

	public void setTradename(String tradename) {
		this.tradename = tradename;
	}

}